package services

import (
	"fiberCommonUtils/request"
	"fmt"
	"github.com/gofiber/fiber/v2"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"mongoDB"
)

// MiddlewareFindService 查询中间件	c.Locals("service")
func MiddlewareFindService(c *fiber.Ctx) error {
	ctx, cancel := mongoDB.MongoDBTimeoutContext()
	defer cancel()
	query := new(MiddlewareServiceFind)
	if c.Method() == "GET" {
		err := c.QueryParser(query)
		if err != nil {
			return fiber.NewError(500, fmt.Sprint("解析错误,", err.Error()))
		}
	} else if c.Method() == "POST" {
		request.ParsePostLikeRequestV2(c, query)
	}
	_service := &Service{}
	err := _service.FindByID(ctx, &query.ServiceID)
	if err != nil {
		return fiber.NewError(500, fmt.Sprint("读取service错误,", err.Error()))
	}
	c.Locals("service", _service)
	return c.Next()
}

type MiddlewareServiceFind struct {
	ServiceID primitive.ObjectID `json:"serviceID" query:"serviceID" form:"serviceID"`
}
